<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class AuthController extends Controller{
    public function auth_table(){
        $rows=DB::table('node')->get();
        $data=[
            "code"=>0,
            "msg"=>'',
            "count"=>count($rows),
            "data"=>$rows
        ];
        return json_encode($data);
    }
    public function auth_add(){
        return view('admin.auth_add');
    }
    //权限递归
    public  function buildMenuChild($pid, $menuList){
        $treeList = [];
        foreach ($menuList as $v) {
            $v->name=$v->title;
            if ($pid == $v->pid) {
                $node = $v;
                $child = $this->buildMenuChild($v->id, $menuList);
                if (!empty($child)) {
                    $node->children = $child;
                }
                // todo 后续此处加上用户的权限判断
                $treeList[] = $node;
            }
        }
        return $treeList;
    }
    public function auth_select(){
        $rows=DB::table('node')
            ->select(["id","pid","title"])
            ->get();
        $data=$this->buildMenuChild(0,$rows);
        return json_encode($data);
    }
    public function auth_insert(Request $request){
        $rows=$request->input();
        $rows['icon']="fa ".$rows['icon'];
        $rows2=DB::table('node')->insert($rows);
        if($rows2){
            return 1;
        }
    }
    public function auth_edit($id){
        $rows=DB::table('node')->where('id',$id)->first();
        return view("admin.auth_edit",["rows"=>$rows]);
    }
    public function auth_update(Request $request){
        $data=$request->post('result');
        $rows=DB::table('node')->where('id',$data['id'])->update($data);
        if($rows){
            return 1;
        }
    }

}